package homework02.exercise2;


//思路:
//写出每个部分的判断方法，在toString中调用 (注:两种实现方式选择一个放进代码块)

//定义一个Ipc类
public class IPC {

    final char SEPARATOR = '/';
    private String section; //部
    private String class_;  //大类
    private String subclass;    //小类
    private String mainGroup;   //大组
    private String subgroup;    //小组
    private int pos;

    private String IPC;

    //含有五个参数的构造器（分别为部、大类、小类、大组和小组）
    public IPC(String section, String class_, String subclass,
               String mainGroup, String subgroup) {
        this.section = section;
        this.class_ = class_;
        this.subclass = subclass;
        this.mainGroup = mainGroup;
        this.subgroup = subgroup;
    }

    //只有一个参数的构造器（以字符串形式表示的IPC分类号）
    public IPC(String IPC) {
        this.IPC = IPC;
    }

    public char getsection() {
        return IPC.charAt(0);
    }

    public Integer getclass() {
        return Integer.parseInt(IPC.substring(1, 3));
    }

    public char getsubclass() {
        return IPC.charAt(3);
    }

    public Integer getPos() {
        int pos = IPC.lastIndexOf(SEPARATOR);
        return IPC.indexOf(SEPARATOR);
    }

    public String getmaingroup() {
        int startIndex = IPC.indexOf("55");
        return IPC.substring(startIndex, startIndex + 2);
    }

    public String getsubgroup() {
        int slashIndex = IPC.indexOf('/');
        return IPC.substring(slashIndex + 1);
    }


    //toString()函数将这五部分拼接成IPC
    @Override
    public String toString() {

        return "IPC号为: " + section +
                class_.format("%2s", class_).replace(' ', '0') + subclass
                + mainGroup.format("%4s", mainGroup) + SEPARATOR + subgroup;
    }


}
